All files / core/snap/indexes PathIndex.ts

100% Statements 33/33
100% Branches 6/6
100% Functions 8/8
100% Lines 31/31
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97                                12x 12x 12x 12x 12x 12x               12x 24x 24x   24x                     12x 504x           12x 13x           12x 252x 252x 252x 252x 76x   176x             12x 2x 2x       2x           12x 5x 5x           12x 327x   12x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { assert } from '@firebase/util';
import { nameCompare, MAX_NAME } from '../../util/util';
import { Index } from './Index';
import { ChildrenNode, MAX_NODE } from '../ChildrenNode';
import { NamedNode, Node } from '../Node';
import { nodeFromJSON } from '../nodeFromJSON';
import { Path } from '../../util/Path';
 
/**
 * @param {!Path} indexPath
 * @constructor
 * @extends {Index}
 */
export class PathIndex extends Index {
  constructor(private indexPath_: Path) {
    super();
 
    assert(
      !indexPath_.isEmpty() && indexPath_.getFront() !== '.priority',
      "Can't create PathIndex with empty path or .priority key"
    );
  }
 
  /**
   * @param {!Node} snap
   * @return {!Node}
   * @protected
   */
  protected extractChild(snap: Node): Node {
    return snap.getChild(this.indexPath_);
  }
 
  /**
   * @inheritDoc
   */
  isDefinedOn(node: Node): boolean {
    return !node.getChild(this.indexPath_).isEmpty();
  }
 
  /**
   * @inheritDoc
   */
  compare(a: NamedNode, b: NamedNode): number {
    const aChild = this.extractChild(a.node);
    const bChild = this.extractChild(b.node);
    const indexCmp = aChild.compareTo(bChild);
    if (indexCmp === 0) {
      return nameCompare(a.name, b.name);
    } else {
      return indexCmp;
    }
  }
 
  /**
   * @inheritDoc
   */
  makePost(indexValue: object, name: string): NamedNode {
    const valueNode = nodeFromJSON(indexValue);
    const node = ChildrenNode.EMPTY_NODE.updateChild(
      this.indexPath_,
      valueNode
    );
    return new NamedNode(name, node);
  }
 
  /**
   * @inheritDoc
   */
  maxPost(): NamedNode {
    const node = ChildrenNode.EMPTY_NODE.updateChild(this.indexPath_, MAX_NODE);
    return new NamedNode(MAX_NAME, node);
  }
 
  /**
   * @inheritDoc
   */
  toString(): string {
    return this.indexPath_.slice().join('/');
  }
}